package guda.sqladmin.web.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import guda.sqladmin.biz.ProjectBiz;
import guda.sqladmin.biz.query.TableQuery;
import guda.sqladmin.dao.ProjectDOMapper;
import guda.sqladmin.dao.domain.ProjectDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import guda.sqladmin.biz.ProjectTableBiz;
import guda.sqladmin.dao.domain.ProjectTableDO;
import guda.sqladmin.web.form.ProjectTableEditForm;
import guda.sqladmin.web.form.ProjectTableForm;
import guda.tools.web.page.BaseQuery;
import guda.tools.web.page.BizResult;
import guda.tools.web.util.RequestUtil;


@Controller
public class ProjectTableAction {


    @Autowired
    private ProjectTableBiz projectTableBiz;
    @Autowired
    private ProjectDOMapper projectDOMapper;

    @RequestMapping(value = "projectTable/list.htm", method = RequestMethod.GET)
    public String list(HttpServletRequest request, ModelMap modelMap) {
        long projectId = RequestUtil.getLong(request,"projectId");
        int pageId = RequestUtil.getInt(request, "pageNo");
        int pageSize = RequestUtil.getInt(request, "pageSize");
        TableQuery tableQuery = new TableQuery();
        tableQuery.setPageNo(pageId);
        tableQuery.setPageSize(pageSize);
        tableQuery.setProjectId(projectId);
        modelMap.put("query",tableQuery);
        BizResult bizResult = projectTableBiz.list(tableQuery);
        ProjectDO projectDO = projectDOMapper.selectByPrimaryKey(projectId);
        modelMap.put("project",projectDO);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "projectTable/list.vm";
        } else {
            return "common/error.vm";
        }

    }

    @RequestMapping(value = "projectTable/edit.htm", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, ModelMap modelMap, ProjectTableEditForm projectTableEditForm,
        BindingResult result, Map<String,Object> model) {

        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = projectTableBiz.detail(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            projectTableEditForm.initForm(((ProjectTableDO)bizResult.data.get("projectTableDO")));
            ProjectDO projectDO = projectDOMapper.selectByPrimaryKey(projectTableEditForm.getProjectId());
            modelMap.put("project",projectDO);
            return "projectTable/edit.vm";
        } else {
            return "common/error.vm";
        }

    }

    @RequestMapping(value = "projectTable/detail.htm", method = RequestMethod.GET)
    public String detail(HttpServletRequest request, ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = projectTableBiz.detail(id);
        if (bizResult.success) {
            ProjectTableDO projectTableDO =  (ProjectTableDO)bizResult.data.get("projectTableDO");
            ProjectDO projectDO = projectDOMapper.selectByPrimaryKey(projectTableDO.getProjectId());
            modelMap.put("project",projectDO);
            modelMap.putAll(bizResult.data);
            return "projectTable/detail.vm";
        } else {
            return "common/error.vm";
        }

    }

    @RequestMapping(value = "projectTable/create.htm", method = RequestMethod.GET)
    public String create(HttpServletRequest request, ModelMap modelMap, ProjectTableForm projectTableForm,
        BindingResult result, Map<String,Object> model) {
        long projectId = RequestUtil.getLong(request,"projectId");
        ProjectDO projectDO = projectDOMapper.selectByPrimaryKey(projectId);
        modelMap.put("project",projectDO);
        projectTableForm.setProjectId(projectId);
        return "projectTable/create.vm";
    }

    @RequestMapping(value = "projectTable/doCreate.htm", method = RequestMethod.POST)
    public String doCreate(HttpServletRequest request, ModelMap modelMap,@Valid  ProjectTableForm projectTableForm,
        BindingResult result, Map<String,Object> model) {
        if (result.hasErrors()) {
            return "projectTable/create.vm";
        }

        ProjectTableDO projectTableDO = projectTableForm.toDO();
        BizResult bizResult = projectTableBiz.create(projectTableDO);
        if (bizResult.success) {
            return "redirect:/projectTable/list.htm";
        } else {
            return "common/error.vm";
        }

    }

    @RequestMapping(value = "projectTable/doUpdate.htm", method = RequestMethod.POST)
    public String doUpdate(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap,@Valid ProjectTableEditForm projectTableEditForm,
        BindingResult result, Map<String,Object> model) {
        if (result.hasErrors()) {
            return "projectTable/edit.vm";
        }
        ProjectTableDO projectTableDO = projectTableEditForm.toDO();
        BizResult bizResult = projectTableBiz.update(projectTableDO);
        if (bizResult.success) {
            return "redirect:/projectTable/list.htm";
        } else {
            return "common/error.vm";
        }

    }

    @RequestMapping(value = "projectTable/doDelete.htm")
    public String doDelete(HttpServletRequest request, ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = projectTableBiz.delete(id);
        if (bizResult.success) {
            return "projectTable/list.htm";
        } else {
            return "common/error.vm";
        }

    }



}
